﻿using MyWPF.Models;
using MyWPF.Models.UserManagement;
using MyWPF.StaticData;
using MyWPF.Views.User;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;

namespace MyWPF.ViewModel.User
{
    public class VM_UserWindow_Login : BaseViewModel
    {

        public VM_UserWindow_Login()
        {
            _close = new DelegateCommand<object>(_Close);
            _min = new DelegateCommand<object>(_Min);
            _moveWindow = new DelegateCommand<object>(_MoveWindow);
            _login = new DelegateCommand<object>(_Login);
        }
        #region 基础：前端事件
        /// <summary>
        /// 关闭窗口
        /// </summary>
        private ICommand _close;
        public ICommand Close
        {
            get { return _close; }
            set
            {
                _close = value;
                OnPropertyChanged("Close");
            }
        }
        private void _Close(object obj)
        {
            StaticModels.userWindow_Login.Close();
        }
        /// <summary>
        /// 最小化窗口
        /// </summary>
        private ICommand _min;
        public ICommand Min
        {
            get { return _min; }
            set
            {
                _min = value;
                OnPropertyChanged("Min");
            }
        }
        private void _Min(object obj)
        {
            StaticModels.userWindow_Login.WindowState = System.Windows.WindowState.Minimized;
        }
        /// <summary>
        /// 移动窗口
        /// </summary>
        private ICommand _moveWindow;
        public ICommand MoveWindow
        {
            get { return _moveWindow; }
            set
            {
                _moveWindow = value;
                OnPropertyChanged("MoveWindow");
            }
        }
        private void _MoveWindow(object obj)
        {
            StaticModels.userWindow_Login.DragMove();
        }

        private void OnPropertyChanged(string v)
        {
            throw new NotImplementedException();
        }
        #endregion


        #region 属性绑定：登录界面显示内容如软件名等
        private string _name;
        public string Name
        {
            get { return _name; }
            set { SetProperty(ref _name, value); }
        }
        private string _passWord;
        public string PassWord
        {
            get { return _passWord; }
            set { SetProperty(ref _passWord, value); }
        }
        private string _winCCName;
        public string WinCCName
        {
            get { return _winCCName; }
            set { SetProperty(ref _winCCName, value); }
        }
        private string _version;
        public string Version
        {
            get { return _version; }
            set { SetProperty(ref _version, value); }
        }
        private string _tips;
        public string Tips
        {
            get { return _tips; }
            set { SetProperty(ref _tips, value); }
        }
        #endregion
        
        #region 事件绑定：登录
        /// <summary>
        /// 登录按钮
        /// </summary>
        private ICommand _login;
        public ICommand Login
        {
            get { return _login; }
            set
            {
                _login = value;
                OnPropertyChanged("Login");
            }
        }
        private void _Login(object obj)
        {
            UserContext User = new UserContext()
            {
                UserName = Name,
                Password = PassWord,
                Errors = 0,
                LastLoginTime = DateTime.Now

            };

            if (User.UserName == "" || User.Password == "" || User.UserName == null || User.Password == null)
            {
                Tips = "用户名或密码不能为空！";
                return;
            }
            var result = StaticModels.user.GetUserStruct_Password(User);
            #region 根据输入密码错误次数来判断多久后可以重新输入
            TimeSpan min3 = new TimeSpan(0, 3, 0);
            TimeSpan min10 = new TimeSpan(0, 10, 0);
            TimeSpan min30 = new TimeSpan(0, 30, 0);
            TimeSpan hour1 = new TimeSpan(1, 0, 0);
            if (result.Length > 0)
            {
                if (StaticModels.user.GetUserStruct_UserName(User)[0].Errors >= 3 && StaticModels.user.GetUserStruct_UserName(User)[0].Errors < 8)
                {
                    DateTime dateTime = Convert.ToDateTime(result[0].ErrorTime);
                    if (DateTime.Now - dateTime < min3)
                    {
                        DateTime opentime = dateTime + min3;
                        Tips = "(" + result[0].Errors + ")该账号因密码输入错误次数过多而被锁定，请 " + opentime.ToString() + " 后再试";
                        return;
                    }
                    else
                    {
                        //登录
                        StaticModels.user.Login(User);

                        //刷新用户管理主界面用户清单
                        StaticModels.vM_UserWindow.UserDataTable = StaticModels.user.GetUser_Level(StaticModels.user.User);
                        //关闭当前登录窗口
                        StaticModels.userWindow_Login.Close();
                        //用户界面刷新
                        StaticModels.vM_UserWindow.ButtonText = "退出登录";
                        StaticModels.vM_UserWindow.CurrentUser = User.UserName;
                    }
                }
                else if (StaticModels.user.GetUserStruct_UserName(User)[0].Errors >= 8 && StaticModels.user.GetUserStruct_UserName(User)[0].Errors < 13)
                {
                    DateTime dateTime = Convert.ToDateTime(result[0].ErrorTime);
                    if (DateTime.Now - dateTime < min10)
                    {
                        DateTime opentime = dateTime + min10;
                        Tips = "(" + result[0].Errors + ")该账号因密码输入错误次数过多而被锁定，请 " + opentime.ToString() + " 后再试";
                        return;
                    }
                    else
                    {
                        //登录
                        StaticModels.user.Login(User);

                        //刷新用户管理主界面用户清单
                        StaticModels.vM_UserWindow.UserDataTable = StaticModels.user.GetUser_Level(StaticModels.user.User);
                        //关闭当前登录窗口
                        StaticModels.userWindow_Login.Close();
                        //用户界面刷新
                        StaticModels.vM_UserWindow.ButtonText = "退出登录";
                        StaticModels.vM_UserWindow.CurrentUser = User.UserName;
                    }
                }
                else if (StaticModels.user.GetUserStruct_UserName(User)[0].Errors >= 13 && StaticModels.user.GetUserStruct_UserName(User)[0].Errors < 18)
                {
                    DateTime dateTime = Convert.ToDateTime(result[0].ErrorTime);
                    if (DateTime.Now - dateTime < min30)
                    {
                        DateTime opentime = dateTime + min30;
                        Tips = "(" + result[0].Errors + ")该账号因密码输入错误次数过多而被锁定，请 " + opentime.ToString() + " 后再试";
                        return;
                    }
                    else
                    {
                        //登录
                        StaticModels.user.Login(User);

                        //刷新用户管理主界面用户清单
                        StaticModels.vM_UserWindow.UserDataTable = StaticModels.user.GetUser_Level(StaticModels.user.User);
                        //关闭当前登录窗口
                        StaticModels.userWindow_Login.Close();
                        //用户界面刷新
                        StaticModels.vM_UserWindow.ButtonText = "退出登录";
                        StaticModels.vM_UserWindow.CurrentUser = User.UserName;
                    }
                }
                else if (StaticModels.user.GetUserStruct_UserName(User)[0].Errors > 18)
                {
                    DateTime dateTime = Convert.ToDateTime(result[0].ErrorTime);
                    if (DateTime.Now - dateTime < hour1)
                    {
                        DateTime opentime = dateTime + hour1;
                        Tips = "(" + result[0].Errors + ")该账号因密码输入错误次数过多而被锁定，请 " + opentime.ToString() + " 后再试";
                        return;
                    }
                    else
                    {
                        //登录
                        StaticModels.user.Login(User);

                        //刷新用户管理主界面用户清单
                        StaticModels.vM_UserWindow.UserDataTable = StaticModels.user.GetUser_Level(StaticModels.user.User);
                        //关闭当前登录窗口
                        StaticModels.userWindow_Login.Close();
                        //用户界面刷新
                        StaticModels.vM_UserWindow.ButtonText = "退出登录";
                        StaticModels.vM_UserWindow.CurrentUser = User.UserName;
                    }
                }
                else
                {
                    

                    //登录
                    StaticModels.user.Login(User);

                    //刷新用户管理主界面用户清单
                    StaticModels.vM_UserWindow.UserDataTable = StaticModels.user.GetUser_Level(StaticModels.user.User);
                    //关闭当前登录窗口
                    StaticModels.userWindow_Login.Close();
                    //用户界面刷新
                    StaticModels.vM_UserWindow.ButtonText = "退出登录";
                    StaticModels.vM_UserWindow.CurrentUser = User.UserName;
                }
            }
            else
            {
                result = StaticModels.user.GetUserStruct_UserName(User);
                if (result.Length > 0)
                {
                    if (result[0].Errors >= 3 && result[0].Errors < 8)
                    {
                        DateTime dateTime = Convert.ToDateTime(result[0].ErrorTime);
                        if (DateTime.Now - dateTime < min3)
                        {
                            DateTime opentime = dateTime + min3;
                            Tips = "(" + result[0].Errors + ")该账号因密码输入错误次数过多而被锁定，请 " + opentime.ToString() + " 后再试";
                            return;
                        }
                    }
                    else if (result[0].Errors >= 8 && result[0].Errors < 13)
                    {
                        DateTime dateTime = Convert.ToDateTime(result[0].ErrorTime);
                        if (DateTime.Now - dateTime < min10)
                        {
                            DateTime opentime = dateTime + min10;
                            Tips = "(" + result[0].Errors + ")该账号因密码输入错误次数过多而被锁定，请 " + opentime.ToString() + " 后再试";
                            return;
                        }
                    }
                    else if (result[0].Errors >= 13 && result[0].Errors < 18)
                    {
                        DateTime dateTime = Convert.ToDateTime(result[0].ErrorTime);
                        if (DateTime.Now - dateTime < min30)
                        {
                            DateTime opentime = dateTime + min30;
                            Tips = "(" + result[0].Errors + ")该账号因密码输入错误次数过多而被锁定，请 " + opentime.ToString() + " 后再试";
                            return;
                        }
                    }
                    else if (result[0].Errors >= 18)
                    {
                        DateTime dateTime = Convert.ToDateTime(result[0].ErrorTime);
                        if (DateTime.Now - dateTime < hour1)
                        {
                            DateTime opentime = dateTime + hour1;
                            Tips = "(" + result[0].Errors + ")该账号因密码输入错误次数过多而被锁定，请 " + opentime.ToString() + " 后再试";
                            return;
                        }
                    }
                    else
                    {
                        StaticModels.user.Login(User);
                        Tips = "用户名或密码错误!";
                    }
                }
                //登录
                StaticModels.user.Login(User);
                Tips = "用户名或密码错误!";
            }
            #endregion
        }

        #endregion
    }
}
